package b1.scenes.fight
{
	import Box2D.Collision.*;
	import Box2D.Collision.Shapes.*;
	import Box2D.Common.Math.*;
	import Box2D.Dynamics.*;
	
	import b1.conifg.*;
	import b1.controls.basic.*;
	import b1.net.*;
	import b1.scenes.*;
	import b1.players.*;
	
	import com.greensock.*;
	
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.text.*;
	import flash.utils.Timer;
	
	public class Crow extends Animal
	{
		//飞的速度
		private var flySpeed:Number = 0;
		//飞行时钟
		private var flyTimer:Timer;
		//扔的骨头
		private var preparedBone:Bone;
		
		public function Crow(animalId:int, leftFlag:Boolean)
		{
			super(animalId, leftFlag);
		}
		
		//准备骨头
		public function prepareBone(boneIndex:int):void 
		{
			//加载骨头
			preparedBone = new Bone(boneIndex);
			var animalInfo:AnimalInfo = PlayerInfoHelper.getInstance().animals[this.animalId];
			var animalConfig:AnimalConfig = Config.animalConfigs[animalInfo.animalIndex];
			if (this.leftFlag)
			{
				preparedBone.x = animalConfig.leftXSite;
				preparedBone.y = animalConfig.leftYSite;
			}
			else
			{
				preparedBone.x = -animalConfig.leftXSite;
				preparedBone.y = animalConfig.leftYSite;
			}
			addChild(preparedBone);
		}
		
		//发射准备的骨头
		public function launchPrepareBone():void
		{
			if (preparedBone == null)
				return;
			var fight:Fight = SceneContainer.getInstance().currentScene as Fight;
			//由若干个骨头集结而成
			var mass:Number = 0;
			var boneConfig:ObjectConfig = Config.objectConfigs[preparedBone.index];
			var existTime:Number = 10;
			var localPoint:Point = new Point(preparedBone.x, preparedBone.y);
			var globalPoint:Point = localToGlobal(localPoint);
			var bonePosX:Number = globalPoint.x;
			var bonePosY:Number = globalPoint.y;
			var bone:Bone; 
			if (boneConfig.splitBoneNum > 0)
			{
				var boneCluster:BoneCluster = new BoneCluster(bonePosX, bonePosY, boneConfig.splitBoneIndex, boneConfig.splitBoneNum);
				for (var i:int = 0; i < boneCluster.bones.length; i++)
				{
					bone = boneCluster.bones[i];
					mass += bone.objectB2Body.GetMass();
					fight.objectLayer.addChild(bone);
					TweenLite.delayedCall(existTime, bone.destroy); 
				} 
				TweenLite.delayedCall(0.2, boneCluster.split);
			}
			else
			{
				bone = new Bone(preparedBone.index);
				var boneb2Body:b2Body = bone.createB2Body(fight.world, bonePosX, bonePosY, bone.width, bone.height, boneConfig.density, boneConfig.restitution);
				boneb2Body.SetAngle(this.rotation * Math.PI / 180);
				TweenLite.delayedCall(existTime, bone.destroy); 
				mass += bone.objectB2Body.GetMass();
				fight.objectLayer.addChild(bone);
			}
			var torque:Number = b2Math.RandomRange(0, 200) * mass;
			var forceX:Number = this.flySpeed * 20 * mass;
			bone.applyForce(forceX, 0, torque)
			if (preparedBone != null && preparedBone.parent != null)
			{
				preparedBone.parent.removeChild(preparedBone);
				preparedBone = null;
			}
		}
		
		//起飞
		public function startFly(speed:Number):void
		{
			this.flySpeed = speed;
			if (leftFlag)
				animalMc.status = 2;
			else
				animalMc.status = 1;
			progressBar.resume();
			//加载时钟
			flyTimer = new Timer(50);
			flyTimer.start();
			flyTimer.addEventListener(TimerEvent.TIMER, onFlyTick);
		}
		
		//停飞
		public function stopFly():void
		{
			if (flyTimer != null)
			{
				flyTimer.removeEventListener(TimerEvent.TIMER, onFlyTick);
				flyTimer.stop();
				flyTimer = null;
			}
		} 
		
		private function onFlyTick(event:TimerEvent):void
		{
			setPosition(this.x + flySpeed, this.y);
		}
		
		//召唤
		public override function summon():void
		{
			super.summon();
			if (leftFlag)
				TweenLite.delayedCall(5, startFly, [2]);
			else
				TweenLite.delayedCall(5, startFly, [-2]);
		}
		
		//进度条到达右侧时候的动作
		public override function onAction():void
		{
			var animalInfo:AnimalInfo = PlayerInfoHelper.getInstance().animals[this.animalId];
			//扔骨头
			prepareBone(animalInfo.userData1);
			launchPrepareBone();
		}
		
		//消失
		public override function unsummon():void
		{
			super.unsummon();
			TweenLite.delayedCall(6, destroy);
		}
		
		public override function destroy():void
		{
			super.destroy();
			stopFly();
			if (this.parent != null)
				this.parent.removeChild(this);
		}
		
	}
}